# 用163邮箱实现登录发邮件操作
import os.path
import time
from selenium import webdriver
from selenium.webdriver.common.by import By


class MailOperation:
    def __init__(self):
        # 实例化配置类
        options = webdriver.ChromeOptions()
        # 准备配置项
        user_data_dir = r'--user-data-dir=C:\Users\戴尔\AppData\Local\Google\Chrome\User Data'
        # 添加参数
        options.add_argument(user_data_dir)
        # 打开浏览器(设置options)
        self.driver = webdriver.Chrome(options=options)
        time.sleep(5)
        self.driver.get('https://mail.163.com')
        self.driver.maximize_window()

    # 免登录进入163邮箱发邮件
    def send_email(self):
        self.driver.find_element(By.ID, '_mail_component_149_149').click()
        time.sleep(3)
        self.driver.find_element(By.CLASS_NAME, 'nui-editableAddr-ipt').send_keys('wl15754375315@163.com')
        time.sleep(3)
        self.driver.find_element(By.XPATH, '//*[contains(@id,"_subjectInput")]').send_keys('我是旺财')
        time.sleep(3)
        # 切换进入iframe标签
        frame1 = self.driver.find_element(By.CLASS_NAME, 'APP-editor-iframe')
        self.driver.switch_to.frame(frame1)
        self.driver.find_element(By.XPATH, '//p[@style="margin:0;"]').send_keys('我是旺财,我天天努力学习')
        time.sleep(3)
        # 回到主页面再点击发送
        self.driver.switch_to.default_content()
        # 添加附件
        file = os.path.join(os.path.dirname(os.path.abspath(__file__)), '01-弹窗处理.py')
        self.driver.find_element(By.CLASS_NAME, 'O0').send_keys(file)
        time.sleep(3)
        # 点击发送
        self.driver.find_element(By.XPATH, '//footer[@class="jp0"]/div[1]').click()

    def quit(self):
        time.sleep(5)
        self.driver.quit()


if __name__ == '__main__':
    case = MailOperation()
    case.send_email()
    case.quit()
